package com.recruit.g1.resume.controller;

import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.pojo.resume.dto.ResumeCreate;
import com.recruit.g1.pojo.resume.vo.ResumeDetail;
import com.recruit.g1.pojo.resume.vo.ResumeInfoVO;
import com.recruit.g1.pojo.resume.vo.ResumeListVO;
import com.recruit.g1.resume.mapper.ResumeMapper;
import com.recruit.g1.resume.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "简历模块")
@RequestMapping("/v1/resume/")
public class ResumeController {
    @Autowired
    ResumeService resumeService;
    @Autowired
    ResumeMapper resumeMapper;
    @GetMapping("getResume")
    @ApiOperation(value = "获取简历列表")
    public JsonResult getResumeList(){
        log.debug("获取简历列表：");
        List<ResumeListVO> resumeList =resumeMapper.selectResumeList();
        return JsonResult.ok(resumeList);
    }

    @GetMapping("/getResumeInfo/{id}")
    @ApiOperation(value = "获取简历信息")
    public JsonResult getResumeInfo(@PathVariable Integer id){
        ResumeDetail resume =resumeMapper.selectResumeById(id);
        return JsonResult.ok(resume);
    }
    @ApiOperation(value = "创建简历")
    @PostMapping("save")
    public JsonResult createResume(@Validated ResumeCreate resumeCreate){
        log.debug("进入简历创建：{}" , resumeCreate);
        if (resumeCreate.getId()==null || resumeCreate.getId()<=0){
            resumeService.createResume(resumeCreate);
            return JsonResult.ok("创建成功");
        }else {
            resumeService.edit(resumeCreate);
            return JsonResult.ok("修改成功");
        }

    }


    @ApiOperation(value = "查询简历信息")
    @GetMapping("select")
    public JsonResult selectResumeById(Long userId){
        log.debug("查询简历信息：");
        List<ResumeInfoVO> list = resumeService.selectByUserId(userId);
        return JsonResult.ok(list);
    }

//    @ApiOperation(value = "编辑简历（选择已创建简历）")
//    @GetMapping("toEdit")
//    public JsonResult getEditResumeById(Long id,@ApiIgnore HttpSession session){
//        log.debug("进入简历编辑：");
//        try {
//
//           /* if (session.getAttribute("currentLoginUser")==null){
//                throw new RuntimeException("没有登陆");
//            }*/
//
//            /**
//             * 剩余逻辑：
//             * 先选择简历，然后查看，
//             * 点击编辑，进入编辑。
//             * 点击保存（创建/更新）。
//             */
//
//            return JsonResult.ok( resumeService.editById(id) );
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return JsonResult.fail("修改失败");
//    }


    @ApiOperation(value = "编辑简历")
    @GetMapping("edit")
    public JsonResult editResumeById(Long id,@ApiIgnore HttpSession session){
        log.debug("进入简历编辑：");
        try {

           /* if (session.getAttribute("currentLoginUser")==null){
                throw new RuntimeException("没有登陆");
            }*/

            /**
             * 剩余逻辑：
             * 先选择简历，然后查看，
             * 点击编辑，进入编辑。
             * 点击保存（创建/更新）。
             */

            /// ///////////////////////////////这里缺少代码

            return JsonResult.ok(  );
        }catch (Exception e){
            e.printStackTrace();
        }

        return JsonResult.fail("修改失败");
    }



















//    @ApiOperation(value = "选择简历")
//    @GetMapping("select")
//    public JsonResult selectResumeById(Long id,@ApiIgnore HttpSession session){
//        try {
//
//            /*if (session.getAttribute("currentLoginUser")==null){
//                throw new RuntimeException("没有登陆");
//            }*/
//            return resumeService.selectById(id);
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return JsonResult.fail("勾选简历失败");
//    }

    @ApiOperation(value = "删除简历")
    @PostMapping("delete")
    public JsonResult deleteById(Long id,@ApiIgnore HttpSession session){
        try {

            /*if (session.getAttribute("currentLoginUser")==null){
                return JsonResult.fail(StateCode.NOT_LOGIN);
            }*/

            log.debug("session.getId的结果->"+session.getAttribute(session.getId()));

            return resumeService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


//    @ApiOperation("我的简历")
//    @GetMapping("myResumeList")
//    public JsonResult myResumeList(@ApiIgnore HttpSession session){
//        UserSaveParam userSaveParam = (UserSaveParam) session.getAttribute("currentLoginUser");
//        if (userSaveParam==null){
//            return JsonResult.fail(StateCode.NOT_LOGIN);
//        }
//        /**
//         *  userSaveParam.getId();
//         */
//        return resumeService.selectByUserId(userSaveParam.getId());
//    }
}
